package com.sky.controller.user;


import com.sky.dto.UserLoginDTO;
import com.sky.pojo.User;
import com.sky.result.Result;
import com.sky.service.user.UserService;
import com.sky.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "用户端用户接口")
@RequestMapping("/user/user")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO dto){
        return userService.login(dto);
    }



    @ApiOperation("缓存demo之查询用户")
    @GetMapping("/selectById")
    @Cacheable(value = "user",key = "#userId") //  value::key
    public User selectById(Long userId){
        return userService.selectById(userId);
    }


    @ApiOperation("缓存demo之保存用户")
    @PostMapping("/save")
    @CachePut(value = "user",key = "#user.id")
    public User save(@RequestBody User user){
        return userService.save(user);
    }

    @ApiOperation("缓存demo之删除用户")
    @DeleteMapping("/delete")
    @CacheEvict(value = "user",key = "#userId")
    public void deleteById(Long userId){
        userService.deleteById(userId);
    }

}
